<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 列表 -->
    <!-- 角色列表区域 -->
    <el-card>
      <!-- 搜索与添加 -->
      <el-row :gutter="20">
        <el-col :span="14">
          <el-button type="primary" @click="addRoleList">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="roleList" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 循环每一行的数据 chldren -->
            <el-row
              :class="['bdbottom',i1==='0'?'':'bdtop','vcenter']"
              :key="item1.id"
              v-for="(item1,i1) in scope.row.children"
            >
              <!-- 渲染一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removeRightById(scope.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级权限和三级 -->
              <el-col :span="19">
                <el-row
                  :class="[i2==='1'?'':'bdtop','vcenter']"
                  v-for="(item2,i2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRightById(scope.row,item2.id)"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="(item3) in item2.children"
                      :key="item3.id"
                      type="warning"
                      closable
                      @close="removeRightById(scope.row,item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>

        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <!-- {{scope.row}} -->
            <el-button type="danger" icon="el-icon-delete"></el-button>
            <el-button type="danger" icon="el-icon-edit"></el-button>
            <el-button type="warning" icon="el-icon-setting" @click="showSetRightDialg(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 权限展示页 -->
    <el-dialog
      title="分配权限"
      :visible.sync="showSetRightVisible"
      width="30%"
      @close="setRightDialogClose(roleId)"
    >
      <el-tree
        :data="rightList"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defKeys"
        :props="treeProps"
        ref="treeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showSetRightVisible = false">取 消</el-button>
        <el-button type="primary" @click="subRightDialog">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      roleList: [],
      showSetRightVisible: false,
      //权限列表
      rightList: [],
      //树形对象的属性绑定事件
      treeProps: {
        label: 'authName',
        childern: 'children'
      },
      //默认选中的三级权限
      defKeys: [],
      //正在操作的用户id
      roleId: ''
    }
  },
  methods: {
    //获取所有的节点//提交数据
    async subRightDialog() {
      //获取所有的节点
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const idStr = keys.join(',')
      console.log(this.roleId)
      console.log(...idStr)

      //发送请求
      const { data: res } = await this.$http.post(
        `roles/${this.roleId}/rights`,
        { rids: idStr }
      )
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('分配权限失败')
      this.$message.success('分配权限成功')
      this.getRoleList()
      //关闭对话框
      this.showSetRightVisible = false
    },
    //关闭对话框
    setRightDialogClose() {
      this.defKeys = []
    },
    //默认选中三级权限
    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
    },
    //   列表查询
    async getRoleList() {
      const { data: res } = await this.$http.get('roles')
      this.roleList = res.data
      if (res.meta.status !== 200) return this.$message.error('查询失败')
      this.$message.success('查询成功')
    },
    //添加列表
    addRoleList() {},
    //根据id删除权限 弹框提示
    async removeRightById(role, rightId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          var { data: res } = await this.$http.delete(
            `roles/${role.id}/rights/${rightId}`
          )
          if (res.meta.status !== 200)
            return this.$message.error('删除角色权限失败')
          console.log(res)
          this.$message.success(res.meta.msg)
          //展开后不和毕 rolr对象的children数据返回 替换 刷新
          role.children = res.data
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //展示分配权限对话框
    async showSetRightDialg(role) {
      const { data: res } = await this.$http.get('/rights/tree')
      console.log(res.data)
      if (res.meta.status !== 200) return this.$message.error('分配失败')
      //数据显示
      this.roleId = role.id
      this.rightList = res.data
      this.getLeafKeys(role, this.defKeys)
      this.showSetRightVisible = true
    }
  },
  created() {
    this.getRoleList()
  }
}
</script>
<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-top: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>